import Vue from "vue";
import VueRouter from "vue-router";
import Home from "@/views/Home.vue";
import NoFind from '@/views/NoFind.vue';
import Logino from "../views/Logino.vue";
Vue.use(VueRouter);

const routes = [{
        path: "/",
        name: "Logino",
        component: Logino

    },
    {
        path: '/home',
        name: 'Home',
        component: Home,
        children: [{
            path: 'all',
            component: () =>
                import ("../components/All")
        }]
    },
    {
        path: '/404',
        name: '404',
        component: NoFind
    },
    {
        path: "/home/:name",
        props: true,
        meta: { title: "home" },
        name: "son",
        component: Home,
        beforeEnter(to, from, next) {
            //to当前的路由配置项
            // from上一个路由配置项
            // next() 中间件 必须写

            console.log(to, from);
            //有权限token就去home没权限就去404
            let token = localStorage.getItem('token')
            if (token) { //没有token
                next()
            } else {
                next({ name: "404" })
            }
            // next()
        }
    },
    {
        path: '/logino',
        name: 'Logino',
        component: Logino
    },
    {
        path: "/about",
        name: "About",
        meta: { title: "about" },
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () =>
            import ( /* webpackChunkName: "about" */ "@/views/About.vue"),
    },
];

const router = new VueRouter({
    routes
})
router.beforeEach((to, from, next) => { //路由守卫不适用循环，因为会死循环
    console.log(to, from);
    //权限认证
    let token = localStorage.getItem('token');
    if (to.path === '/Logino') {
        next()
    } else {
        //非登录页，需要权限
        if (token) { //有权限直接下一步
            next()
        } else { //没有权限去获得权限
            next({ path: '/Logino' })
        }
    }
    next();
})
export default router;